﻿using System;
using System.Collections.Generic;
using System.Text;
using SdmxMl.Structure;
using SdmxMl.Common;

namespace SakeVar
{
    public class VarGroup
    {
        public ISdmxHierarchyListItem ACT { get; private set; }
        public ISdmxHierarchyListItem APP { get; private set; }
        public ISdmxHierarchyListItem ACS { get; private set; }
        public ISdmxHierarchyListItem CHO { get; private set; }
        public ISdmxHierarchyListItem NA  { get; private set; }

        private bool isModule;

        public VarGroup(bool module)
        {
            isModule = module;
        }
        public void Add(ISdmxHierarchyListItem item)
        {
            // Get concerned group(s)
            Annotation a = item.AnnotationList.GetAnnotationType("GROUP");
            if (a == null)
            {
                //throw new ApplicationException("Undefined group(s) for: " + item.Id);
                a = new Annotation("GROUP");
                a.Title = "MOD";
            }

            string grpList = a.Title;
            string[] arrGroup = grpList.Split(',');
            foreach (string grp in arrGroup)
            {
                switch (grp.Trim())
                {
                    case "MOD":
                        if (ACT != null || APP != null || ACS != null || CHO != null || NA != null)
                            throw new ApplicationException("Invalid group attribution (MOD) for " + item.Id);
                        if (isModule)
                            ACT = item;
                        else
                        {
                            ACT = APP = ACS = CHO = NA = item;
                        }
                        break;

                    case "TOT":
                        if (ACT != null || APP != null || ACS != null || CHO != null || NA != null)
                            throw new ApplicationException ("Invalid group attribution (TOT) for " + item.Id);
                        ACT = APP = ACS = CHO = NA = item;
                        break;

                    case "ACT":
                         if (ACT != null)
                            throw new ApplicationException ("Invalid group attribution (ACT) for " + item.Id);
                        ACT = item;
                       break;

                    case "APP":
                           if (APP != null)
                            throw new ApplicationException ("Invalid group attribution (APP) for " + item.Id);
                        APP = item;
                      break;

                    case "ACS":
                           if (ACS != null)
                            throw new ApplicationException ("Invalid group attribution (ACS) for " + item.Id);
                        ACS = item;
                        break;

                    case "CHO":
                           if (CHO != null)
                            throw new ApplicationException ("Invalid group attribution (CHO) for " + item.Id);
                        CHO = item;
                        break;
                    case "NA":
                            if (NA != null)
                            throw new ApplicationException ("Invalid group attribution (NA) for " + item.Id);
                        NA = item;
                       break;
                    default:
                        throw new ApplicationException("Unknown Group: " + grp + " in variable " + item.Id);

                }
            }
        }

        public string GetVarId(string domain)
        {
            string id = string.Empty;
            switch (domain)
            {
                case "ACT":
                    if (ACT != null)
                        id = ACT.Id;
                    break;

                case "APP":
                    if (APP != null)
                        id = APP.Id;
                    break;

                case "ACS":
                    if (ACS != null)
                        id = ACS.Id;
                    break;

                case "CHO":
                    if (CHO != null)
                        id = CHO.Id;
                    break;
                case "NA":
                    if (NA != null)
                        id = NA.Id;
                    break;
            }

            return id;
        }
        public string GetQuestPrint(string domain)
        {
            string qp = string.Empty;
            switch (domain)
            {
                case "ACT":
                    qp = GetQPrint(ACT);
                    break;

                case "APP":
                    qp = GetQPrint(APP);
                    break;

                case "ACS":
                    qp = GetQPrint(ACS);
                    break;

                case "CHO":
                    qp = GetQPrint(CHO);
                    break;
                case "NA":
                    qp = GetQPrint(NA);
                    break;
            }

            return qp;
        }

        private string GetQPrint(ISdmxHierarchyListItem item)
        {
            string qp = string.Empty;
            if (item != null)
            {
                Annotation a = item.AnnotationList.GetAnnotationType("QUEST_PRINT");
                if (a != null)
                    qp = a.Title;
            }


            return qp;
        }

        private List<ISdmxHierarchyListItem> ConstructItemList()
        {
            List<ISdmxHierarchyListItem> list = new List<ISdmxHierarchyListItem>();
            list.Add(ACT);
            list.Add(APP);
            list.Add(ACS);
            list.Add(CHO);
            list.Add(NA);

            return list;
        }

        public string GetWaveInfo(ref string periodicity)
        {
            List<ISdmxHierarchyListItem> list = ConstructItemList();

            string wave = string.Empty;
            string annuel = string.Empty;

            foreach (ISdmxHierarchyListItem item in list)
            {
                string anu = string.Empty;
                string w = string.Empty;

                w = GetPeriod(item, ref anu);
                if (anu.Length > 0 && annuel.Length > 0 && annuel != anu)
                    throw new ApplicationException("Incompatible period detected in " + item.Id);
                if (w.Length > 0 && wave.Length > 0 && wave != w)
                    throw new ApplicationException("Incompatible wave detected in " + item.Id);

                if (w.Length > 0)
                    wave = w;
                if (anu.Length > 0)
                    annuel = anu;
            }

            periodicity = annuel;
            return wave;

        }

        public string GetIndicSup()
        {
            List<ISdmxHierarchyListItem> list = ConstructItemList();

            string sup = string.Empty;
            foreach (ISdmxHierarchyListItem item in list)
            {
                string s = GetSup(item);
                if (item != null && sup.Length > 0 && s != sup)
                    throw new ApplicationException("Incompatible indication sup in " + item.Id);
                if (s.Length > 0)
                    sup = s;
            }
            return sup;
        }
        

        public static string GetPeriod(ISdmxHierarchyListItem item, ref string annuel)
        {
            string period = string.Empty;
            if (item != null)
            {
                Annotation a = item.AnnotationList.GetAnnotationType("VAGUE");
                if (a == null)
                {
                    // throw new ApplicationException("VAGUE annotation missing for: " + item.Id);
                    a = new Annotation("VAGUE");
                    a.Title = "1";
                }
                period = a.Title;
                if (period.IndexOf('-') > 0)
                    annuel = LangSupport.CurLang == "de" ? "Vierteljährlich" : "Trimestrielle";
                else
                    annuel = LangSupport.CurLang == "de" ? "Jährlich" : "Annuelle";
            }
            return period;
        }

        public static string GetSup(ISdmxHierarchyListItem item)
        {
            string sup = string.Empty;
            if (item != null)
            {
                Annotation a = item.AnnotationList.GetAnnotationType("INDIC_SUP");
                if (a != null)
                    sup = a.Title;
            }
            return sup;
        }

    }
    


}
